AC_DEFUN([LX_CHECK_HDF],
[
  AC_ARG_WITH([hdf],
AS_HELP_STRING([--with-hdf@<:@=PREFIX@:>@], [search for HDF under PREFIX or under the default search paths if PREFIX is not given @<:@default@:>@])
AS_HELP_STRING([--without-hdf], [disable checking for HDF]),
              [], [with_hdf=yes])

  AC_ARG_WITH([hdf-includedir],
AS_HELP_STRING([--with-hdf-includedir=DIR], [search for HDF headers in DIR]),
              [], [with_hdf_includedir=no])

  AC_ARG_WITH([hdf-libdir],
AS_HELP_STRING([--with-hdf-libdir=DIR], [search for HDF libraries in DIR]),
              [], [with_hdf_libdir=no])

  lx_hdf_found=no

  AC_MSG_CHECKING([for HDF])

  if test x"$with_hdf_includedir" != x"no"; then
    HDF_CFLAGS="-I$with_hdf_includedir"
  elif test x"$with_hdf" != x"yes"; then
    HDF_CFLAGS="-I$with_hdf/include"
  fi

  if test x"$with_hdf_libdir" != x"no"; then
    HDF_LDFLAGS="-L$with_hdf_libdir"
  elif test x"$with_hdf" != x"yes"; then
    HDF_LDFLAGS="-L$with_hdf/lib"
  fi
  HDF_LIBS="-lmfhdf -ldf -ljpeg -lz"

  lx_save_cxxflags="$CXXFLAGS"
  lx_save_ldflags="$LDFLAGS"
  lx_save_libs="$LIBS"
  CXXFLAGS="$HDF_CFLAGS"
  LDFLAGS="$HDF_LDFLAGS"
  LIBS="$HDF_LIBS"

  lx_hdf_test_prog='
    extern "C" {
    #include <hdf/mfhdf.h>
    }

    int main(int argc, char** argv)
    {
      return 0;
    }'

  AC_LANG_PUSH(C++)
  AC_LINK_IFELSE([$lx_hdf_test_prog], [lx_hdf_found=yes], [lx_hdf_found=no])
  AC_LANG_POP(C++)

  CXXFLAGS="$lx_save_cxxflags"
  LDFLAGS="$lx_save_ldflags"
  LIBS="$lx_save_libs"

  if test x"$lx_hdf_found" = x"no"; then
    AC_MSG_ERROR([LIBHDF missing.])
  fi

  AC_DEFINE([HAVE_HDF], [1], [Define to 1 if you have HDF.])
  AC_MSG_RESULT([yes])

  HDF_LIBS="$HDF_LDFLAGS $HDF_LIBS"
  AC_SUBST(HDF_CFLAGS)
  AC_SUBST(HDF_LIBS)
  AM_CONDITIONAL([HAVE_HDF], [test x"$lx_hdf_found" = x"yes"])
])
